package com.sunspring.zdcall.controller.commoncontroller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sunspring.zdcall.common.SimpleResponse;
import com.sunspring.zdcall.common.constant.PageConstant;
import com.sunspring.zdcall.common.exception.ZdException;
import com.sunspring.zdcall.dto.UserDto;
import com.sunspring.zdcall.entity.ZdLoginLog;
import com.sunspring.zdcall.param.UserParam;
import com.sunspring.zdcall.param.UserSearchParam;
import com.sunspring.zdcall.service.LoginLogService;
import com.sunspring.zdcall.service.UserService;
import com.sunspring.zdcall.support.ContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.sql.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author WuLei
 * @Date 2018/12/3  11:54
 */
@RestController
@RequestMapping("/sys/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private LoginLogService lls;


    /**
     * 修改密码
     * @param oldPwd 旧密码
     * @param newPwd 新密码
     */
    @PostMapping("/verifyPwd")
    public SimpleResponse verifyPwd(String oldPwd,String newPwd) {
        String currentUsername = ContextHolder.get().getUsername();
        userService.verifyPwd(currentUsername,oldPwd,newPwd);
        return SimpleResponse.success();
    }

    /**
     * 查询用户信息
     * @param usp 条件参数
     * @param pageNum 页码
     */
    @GetMapping("/info")
    public SimpleResponse info(UserSearchParam usp,Integer pageNum) {
        if (pageNum == null || pageNum <= 0) {
            throw new ZdException("页码错误");
        }
        PageHelper.startPage(pageNum, PageConstant.PAGE_SIZE);
        List<UserDto> list = userService.selectAll(usp);
        if (CollectionUtils.isEmpty(list)) {
            return SimpleResponse.success();
        }
        //分页
        PageInfo<UserDto> page= new PageInfo<>(list);
        return SimpleResponse.success(page);
    }

    /**
     * 新增用户
     * @param userParam userParam参数实体
     */
    @PostMapping("/add")
    public SimpleResponse addUser(@Valid UserParam userParam, BindingResult bindingResult) {
        //参数校验
        if (bindingResult.hasErrors()) {
            throw new ZdException(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        userService.addNewUser(userParam);
        return SimpleResponse.success();
    }


    /**
     * 更新用户信息 （更新所属检查室和科室）
     * @param userId 用户id
     * @param checkroomIds 检查室Id集合
     */
    @PostMapping("/update")
    public SimpleResponse update(int userId,String checkroomIds) {
        userService.updateUser(userId,checkroomIds);
        return SimpleResponse.success();
    }

    /**
     * 停用/启用
     * @param  userId 用户ID
     */
    @PostMapping("/switch")
    public SimpleResponse changeState(Integer userId) {
        userService.changeState(userId);
        return SimpleResponse.success();
    }

    /**
     * 重置密码
     * @param userId 用户id
     */
    @PostMapping("/resetPwd")
    public SimpleResponse resetPwd(Integer userId) {
        userService.resetPwd(userId);
        return SimpleResponse.success();
    }


    /**
     * 查看登录日志
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param pageNum 页码
     */
    @GetMapping("/loginLog")
    public SimpleResponse loginLog(Date startTime, Date endTime,int pageNum) {
        PageHelper.startPage(pageNum, PageConstant.PAGE_SIZE);
        List<ZdLoginLog> list = lls.selectAll(startTime,endTime);
        if (CollectionUtils.isEmpty(list)) {
            return SimpleResponse.success();
        }
        //分页
        PageInfo<ZdLoginLog> page= new PageInfo<>(list);
        return SimpleResponse.success(page);
    }
 }
